package com.mdd.admin.service.recharge.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.recharge.IRechargeOrderService;
import com.mdd.admin.validate.common.PageValidate;
import com.mdd.admin.validate.finance.FinanceRechargeSearchValidate;
import com.mdd.admin.vo.finance.FinanceRechargeListVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.recharge.RechargeOrder;
import com.mdd.common.entity.refund.OrderRefund;
import com.mdd.common.enums.PaymentEnum;
import com.mdd.common.enums.RefundEnum;
import com.mdd.common.mapper.recharge.RechargeOrderMapper;
import com.mdd.common.mapper.refund.OrderRefundMapper;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RechargeOrderServiceImpl implements IRechargeOrderService {

    @Resource
    private RechargeOrderMapper rechargeOrderMapper;

    @Resource
    private OrderRefundMapper orderRefundMapper;

    /**
     * 充值记录
     *
     * @author fzr
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<FinanceRechargeListVo>
     */
    @Override
    public PageResult<FinanceRechargeListVo> list(PageValidate pageValidate, FinanceRechargeSearchValidate searchValidate) {
        Integer pageNo = pageValidate.getPageNo();
        Integer pageSize = pageValidate.getPageSize();

        MPJQueryWrapper<RechargeOrder> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(RechargeOrder.class)
                .select("U.id as user_id,U.sn as user_sn,U.nickname,U.avatar,U.username as username")
                .leftJoin("?_user U ON U.id=t.user_id".replace("?_", GlobalConfig.tablePrefix))
                .orderByDesc("id");

        if (StringUtil.isNotEmpty(searchValidate.getKeyword())) {
            String keyword = searchValidate.getKeyword();
            mpjQueryWrapper.nested(wq->wq
                    .like("U.nickname", keyword).or()
                    .like("U.username", keyword).or()
                    .like("U.mobile", keyword));
        }

        rechargeOrderMapper.setSearch(mpjQueryWrapper, searchValidate, new String[]{
                "like:sn@t.order_sn:str",
                "=:payWay@t.pay_way:int",
                "=:payStatus@t.pay_status:int",
                "datetime:startTime-endTime@t.create_time:str",
        });

        IPage<FinanceRechargeListVo> iPage = rechargeOrderMapper.selectJoinPage(
                new Page<>(pageNo, pageSize),
                FinanceRechargeListVo.class,
                mpjQueryWrapper);

        for (FinanceRechargeListVo vo : iPage.getRecords()) {
            vo.setCreateTime(TimeUtil.timestampToDate(vo.getCreateTime()));
            vo.setPayTime(vo.getPayTime().equals("0") ? "-" : TimeUtil.timestampToDate(vo.getPayTime()));
            vo.setAvatar(UrlUtil.toAbsoluteUrl(vo.getAvatar()));
            vo.setPayWay(PaymentEnum.getPayWayMsg(Integer.parseInt(vo.getPayWay())));
            vo.setIsRefund(0);
            if (vo.getPayStatus().equals(1)) {
                OrderRefund refundRecord = orderRefundMapper.selectOne(
                        new QueryWrapper<OrderRefund>()
                                .eq("order_type", "recharge")
                                .eq("order_id", vo.getId())
                                .last("limit 1"));
                if (StringUtil.isNotNull(refundRecord)) {
                    vo.setIsRefund(1);
                    vo.setRefundStatusMsg(RefundEnum.getRefundStatusMsg(refundRecord.getRefundStatus()));
                }
            }
        }

        return PageResult.iPageHandle(iPage);
    }
}
